//
//  ChooseCalenderDateViewController.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/7/10.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "ChooseCalenderDateViewController.h"
#import "GTLCalendarView.h"


@interface ChooseCalenderDateViewController () <GTLCalendarViewDataSource, GTLCalendarViewDelegate>
{
    NSString *_startDate;
    NSString *_endDate;
}
@end

@implementation ChooseCalenderDateViewController

#pragma mark - GTLCalendarViewDataSource

- (NSDate *)minimumDateForGTLCalendar {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    NSString *minimumD = @"2017-06-01";
    return [dateFormatter dateFromString:minimumD];
}

- (NSDate *)maximumDateForGTLCalendar {
    return [NSDate new];
}

- (NSDate *)defaultSelectFromDate {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    
    NSString *toDate ;
    if ([BGFunctionHelper isNULLOfString:_start]) {
        toDate = [BGDateHelper seeDayAroundToday:-8][0];
        
        NSString *startLongLong = [BGDateHelper getTimeStempByString:toDate havehh:NO];
        NSString *beginLongLong = [BGDateHelper getTimeStempByString:_beginDate havehh:NO];
        if (beginLongLong.longLongValue > startLongLong.longLongValue) {
            toDate = _beginDate;
        }
        
    }else{
        toDate = _start;
    }
    _startDate = toDate;
    return [dateFormatter dateFromString:toDate];
}

- (NSDate *)defaultSelectToDate {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    NSString *toDate ;
    if ([BGFunctionHelper isNULLOfString:_end]) {
        toDate = [BGDateHelper seeDayAroundToday:-1][0];
    }else{
        toDate = _end;
    }
    _endDate = toDate;
    return [dateFormatter dateFromString:toDate];
}

#pragma mark - GTLCalendarViewDelegate

- (NSInteger)rangeDaysForGTLCalendar {
    return 30 * 6;
}

- (void)selectNSStringFromDate:(NSString *)fromDate toDate:(NSString *)toDate {
    NSLog(@"fromDate: %@, toDate: %@", fromDate, toDate);
    _startDate = fromDate;
    _endDate = toDate;
}

#pragma mark - private instance method

#pragma mark * init values

- (void)setupGTLCalendarViews {
    CGRect frame = CGRectMake(0, 0, _viewContent.sizeWidth, _viewContent.sizeHeight);
    
    GTLCalendarView *gtlCalendarView = [[GTLCalendarView alloc] initWithFrame:frame];
    gtlCalendarView.dataSource = self;
    gtlCalendarView.delegate = self;
    [_viewContent addSubview:gtlCalendarView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupGTLCalendarViews];
}


#pragma mark - 点击事件

- (IBAction)backButtonClick:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)sureButtonClick:(UIButton *)sender {
    [_delegate getStartDate:_startDate endDate:_endDate];
    [self backButtonClick:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
